// pages/profile/profile.js
Page({
  data: {
    userInfo: {
      nickname: '未设置昵称',
      id: '130****4729',
      avatar: ''
    },
    profileCount: 0
  },

  // 头像加载错误处理
  onAvatarError() {
    this.setData({
      'userInfo.avatar': ''
    });
  },

  onLoad: function (options) {
    this.loadUserInfo();
  },

  onShow: function () {
    // 页面显示时刷新用户信息
    this.loadUserInfo();
    this.loadProfileCount();
  },

  // 加载用户信息
  loadUserInfo: function() {
    const userAvatarUrl = wx.getStorageSync('userAvatarUrl');
    const userNickName = wx.getStorageSync('userNickName');
    const hasUserInfo = wx.getStorageSync('hasUserInfo');
    
    if (hasUserInfo && userAvatarUrl && userNickName) {
      this.setData({
        'userInfo.avatar': userAvatarUrl,
        'userInfo.nickname': userNickName
      });
    }
  },

  // 加载档案数量
  loadProfileCount: function() {
    const profiles = wx.getStorageSync('profiles') || [];
    this.setData({
      profileCount: profiles.length
    });
  },

  // 跳转到档案信息列表页面
  goToProfileList: function() {
    wx.navigateTo({
      url: '/pages/profile-list/profile-list'
    });
  }
});